pipeline {
    agent any
    stages {
        stage('pull master') {
            steps {
                //git credentialsId: 'GitLabSSHDemo', url: 'ssh://git@gitlab.com/xxxx/xxxx.git'
                git credentialsId: 'DockerGitSSHKeyDemo', url: 'ssh://git@ip:port/xxxx/xxxx.git'
            }
        }
        stage('commit info') { 
            steps {
                sh '''sha1=`git rev-parse remotes/origin/master^{commit}`
                name=`git show $sha1`
                '''
            }
        }
        stage('del dev branch') { 
            steps {
                script {
                    sh "git checkout master"
                    def originBranch = sh(script: "git branch", returnStdout: true)
                    echo "local dev branch: $originBranch"
                    if (originBranch.contains('dev')) {
                        sh "git branch |grep 'dev'|xargs git branch -d"
                    }else {
                        echo "no dev"
                    }
                }
            }
        }
        stage('push tag') { 
            input {
                message "input: version/commit"
                ok "OK"
                //submitter "alice,bob"
                parameters {
                    string(name: 'TAGVERSION', defaultValue: '', description: 'version(eg:1.0.0)')
                    string(name: 'TAGCOMMIT', defaultValue: '', description: 'commit')
                    booleanParam(name: 'HOTFIX', defaultValue: false, description: 'is hotfix（default false）') 
                }
            }
            steps {
                sh '''git fetch
                version=${TAGVERSION}
                commit=${TAGCOMMIT}
                isHotFix=${HOTFIX}
                if [ -n "$version" ] || [ -n "$commit" ];
                then
                    currentTime=`date +%Y%m%d%H%M%S`
                    newVersion='tag_release_v'$version'.'$currentTime
                    if [ $isHotFix = 'true' ];
                    then
                        newVersion=$newVersion'_hotfix'
                    fi
                    echo "tag name: ${newVersion}"
                    echo "commit info: ${commit}"
                    git tag -a $newVersion -m "description: ${commit}"
                    git push origin $newVersion
                else
                    echo "error, version or commit null"
                    exit 1
                fi
                '''
                // sh '''git fetch
                //     currentTime=`date +%Y%m%d%H%M%S`
                //     newVersion='tag_'$currentTime
                //     git tag -a $newVersion -m 'new tag'
                //     git push origin $newVersion
                //    '''
            }
        }
        stage('merge dev branch') {
          steps {
                script {
                    def branches = [:]
                    MAX_CONCURRENT = 1
                    //new fifo
                    latch = new java.util.concurrent.LinkedBlockingDeque(MAX_CONCURRENT)
                     
                    //fifo max
                    for(int i=0; i<MAX_CONCURRENT; i++)
                       latch.offer("$i")

                    sh "git fetch -p"
                    
                    def allBranch = sh(script: "git branch -a", returnStdout: true)
                    def job_list = allBranch.split(" ") as List

                    for(int i=0; i<job_list.size(); i++) {
                        def name = job_list[i]
                        if (name.contains('origin/dev')) {
                            print "branch_name = $name"
                            def item_list = name.split('/') as List
                            def branch = item_list[-1]
                            branches[branch] = {
                                def thing = null
                                waitUntil {
                                   //get thing
                                   thing = latch.pollFirst()
                                   return thing != null
                                }
                                try {
                                    //run job
                                    print branch
                                    sh "git checkout $branch"
                                    sh "git pull --ff-only"
                                    sh "git merge master"
                                    sh "git push -u origin $branch"
                                }
                                finally {
                                    //release thing
                                    latch.offer(thing)
                                }        
                            }
                        }

                    }
                    timestamps {
                        parallel branches
                    }
                }
            }
        }
        stage('branch -v') { 
            steps {
                sh '''git branch -v
                   '''
            }
        }
    }

}